package com.cn.bulu.TEST;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.cn.bulu.utils.alipay.config.AlipayConfig;
import com.cn.bulu.utils.alipay.sign.RSA;
import com.cn.bulu.utils.alipay.util.AlipayCore;

/**
 * 
 * @author Administrator
 *
 */
public class AlipayTest {
	// 測試阿里云支付商品
	@Test
	public void test1() throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("partner", AlipayConfig.app_id);
		params.put("service", AlipayConfig.service);
		params.put("seller_id", AlipayConfig.seller);
		params.put("out_trade_no", "00000001");
		params.put("subject", "测试商品test1");
		params.put("body", "测试商品详情test1");
		params.put("total_fee", "0.01");
		params.put("notify_url", AlipayConfig.notifyUrl);
		params.put("_input_charset", "utf-8");
		String orderStr = AlipayCore.createLinkString(params);
		String rsa_sign = URLEncoder.encode(RSA.sign(orderStr, AlipayConfig.app_private_key, AlipayConfig.input_charset),
				AlipayConfig.input_charset);
		orderStr = orderStr + "&sign=\"" + rsa_sign + "\"&sign_type=\"" + AlipayConfig.sign_type + "\"";
		System.out.println("签名的字符串是：" + orderStr);
	}

	@Test
	public void test2() {
		// 实例化客户端
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
				AlipayConfig.app_id, AlipayConfig.app_private_key, "json", "utf-8", AlipayConfig.alipay_public_key, "RSA2");
		// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		// SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody("我是测试数据test2");
		model.setSubject("App支付测试test2");
		model.setOutTradeNo("00000003");
		model.setTimeoutExpress("30m");
		model.setTotalAmount("0.01");
		model.setProductCode("QUICK_MSECURITY_PAY");
		request.setBizModel(model);
		
		
		try {
			AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
			System.out.println(
					"华丽丽的分割线==================================================================================================");
			System.out.println(response.getBody());// 就是orderString
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
	}
}
